package com.sxm.yiwei.adapters.pinche;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.sxm.yiwei.R;
import com.sxm.yiwei.beans.pinche.HostOrder;
import com.sxm.yiwei.constant.Common;
import com.sxm.yiwei.constant.Constant;
import com.sxm.yiwei.tools.CircleTransform;
import com.sxm.yiwei.views.AmountView;
import com.sxm.yiwei.views.CircleImage;

import org.xutils.x;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/10/24.
 */

public class HostOrdersAdapter extends BaseAdapter {
    private final LayoutInflater inflater;
    private Context context;
    private int from;
    private ArrayList<HostOrder> list;
    private OnTicketChangeListener mOnTicketChangeListener;
    private OnBuyClickListener mOnBuyClickListener;
    public HostOrdersAdapter(Context context,ArrayList<HostOrder> list,int from) {
        this.context=context;
        this.list=list;
        this.from=from;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        if (from==0){//HostOrderFragment
            ViewHolder holder=null;
            if (view==null){
                view=inflater.inflate(R.layout.host_order_list_item,null);
                holder=new ViewHolder();
                holder.iv_icon=(CircleImage) view.findViewById(R.id.iv_icon);
                holder.tv_phone=(TextView)view.findViewById(R.id.tv_phone);
                holder.tv_is_online=(TextView)view.findViewById(R.id.tv_is_online);
                holder.tv_words=(TextView)view.findViewById(R.id.tv_words);
                holder.tv_time=(TextView)view.findViewById(R.id.tv_recent_come);
                holder.tv_start_pos=(TextView)view.findViewById(R.id.tv_start_pos);
                holder.tv_end_pos=(TextView)view.findViewById(R.id.tv_end_pos);
                holder.tv_price=(TextView)view.findViewById(R.id.tv_single_price);
                holder.tv_seats=(TextView)view.findViewById(R.id.tv_transfer_fee);
                holder.tv_break_appointment_money=(TextView)view.findViewById(R.id.tv_loss_money);
                holder.tv_depart_time=(TextView)view.findViewById(R.id.tv_depart_time);
                holder.btn_wei=(Button)view.findViewById(R.id.btn_wei);
                holder.btn_buy=(Button)view.findViewById(R.id.btn_buy);
                view.setTag(holder);
            }else {
                holder= (ViewHolder) view.getTag();
            }
            HostOrder hostOrder = list.get(i);

            x.image().bind(holder.iv_icon, Common.IMAGE_HOST+hostOrder.getIcon());
            String phone = hostOrder.getPhone();
            phone=phone.substring(0,3)+"****"+phone.substring(7,11);
            holder.tv_phone.setText(phone);
       /* switch (hostOrder.getIs_online()){
            case 0:
                holder.tv_is_online.setText("[在线]");
                holder.tv_is_online.setTextColor(context.getResources().getColor(R.color.color_online));
                break;
            case 1:
                holder.tv_is_online.setText("[离线]");
                holder.tv_is_online.setTextColor(context.getResources().getColor(R.color.color_offline));
                break;
        }*/
            if (TextUtils.isEmpty(hostOrder.getWords())){
                holder.tv_words.setText("车主留言：车主很懒，没有留言");

            }else {
                holder.tv_words.setText("车主留言："+hostOrder.getWords());
            }
            holder.tv_words.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.mipmap.chezhu),null,null,null);

            holder.tv_time.setText(hostOrder.getStart_time());
            holder.tv_start_pos.setText(hostOrder.getStart_place());
            holder.tv_end_pos.setText(hostOrder.getEnd_place());
            holder.tv_price.setText(hostOrder.getPrice()+"");
            holder.tv_seats.setText(hostOrder.getSeats()+"");
            holder.tv_break_appointment_money.setText(hostOrder.getBreak_money()+"");
            holder.tv_depart_time.setText(hostOrder.getDepart_time());
            holder.btn_buy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context,"购票  "+i,Toast.LENGTH_SHORT).show();
                    mOnBuyClickListener.onBuyClick(i);
                }
            });
            holder.btn_wei.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context,"喂一下  "+i,Toast.LENGTH_SHORT).show();

                }
            });
        }else {//SelectChangeRouteAcitvity
            final ViewHolder2 holder2;
            if (view==null){
                view=inflater.inflate(R.layout.select_route_item,null);
                holder2=new ViewHolder2(view);
                view.setTag(holder2);
            }else {
                holder2=(ViewHolder2)view.getTag();
            }
            HostOrder hostOrder = list.get(i);
            Picasso.with(context).load(Common.IMAGE_HOST+hostOrder.getIcon()).error(R.mipmap.morentouxiang).transform(new CircleTransform()).into(holder2.icon);
            holder2.phone_style.setText(hostOrder.getPhone());
            if (hostOrder.getVehicle_type()==Constant.BUS){
                holder2.phone_style.setCompoundDrawablesWithIntrinsicBounds(null,null,context.getResources().getDrawable(R.mipmap.bus),null);
            }else {
                holder2.phone_style.setCompoundDrawablesWithIntrinsicBounds(null,null,context.getResources().getDrawable(R.mipmap.car1),null);
            }
            holder2.words.setText("留言："+hostOrder.getWords());
            holder2.start_pos.setText(hostOrder.getStart_place());
            holder2.end_pos.setText(hostOrder.getEnd_place());
            holder2.single_price.setText(hostOrder.getSingle_money()+"");
            holder2.seats.setText(hostOrder.getSeats()+"");
            holder2.loss.setText(hostOrder.getLoss_money()+"");
            holder2.depart_time.setText(hostOrder.getDepart_time());

            holder2.amountView.setGoods_storage(hostOrder.getSeats());
            holder2.amountView.setTag(hostOrder);
            holder2.checkBox.setTag(hostOrder);
            holder2.amountView.clearFocus();
            holder2.checkBox.clearFocus();
            if (hostOrder.isSelected()){
                holder2.cardView.setBackgroundColor(context.getResources().getColor(R.color.colorAb));
                holder2.checkBox.setChecked(true);

            }else {
                holder2.cardView.setBackgroundColor(context.getResources().getColor(R.color.select_route_color));
                holder2.checkBox.setChecked(false);

            }


            if (hostOrder.getHttpAction()== Constant.DEFAULT||hostOrder.getHttpAction()==Constant.REFRESH){
                //初始化checkBox选中状态,改签票数
                holder2.checkBox.setSelected(false);
                holder2.amountView.afterTextChanged(new SpannableStringBuilder("1"));

            }else {

            }
            holder2.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if (b){
                        holder2.cardView.setBackgroundColor(context.getResources().getColor(R.color.colorAb));
                    }else {
                        holder2.cardView.setBackgroundColor(context.getResources().getColor(R.color.select_route_color));
                    }
                    HostOrder order = (HostOrder) holder2.checkBox.getTag();
                    order.setSelected(b);
                    mOnTicketChangeListener.onCheckedChange(b,i);
                }
            });
            holder2.amountView.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
                @Override
                public void onAmountChange(View view, int amount) {
                    //Toast.makeText(context,amount+"",Toast.LENGTH_SHORT).show();
                    HostOrder order = (HostOrder) holder2.amountView.getTag();
                    mOnTicketChangeListener.onTicketChange(order.getTickets(),amount,i);
                    order.setTickets(amount);
                }
            });

            holder2.seats.setText(hostOrder.getSeats()+"");

        }


        return view;
    }
    private class ViewHolder{
        CircleImage iv_icon;
        TextView tv_phone;
        TextView tv_is_online;
        TextView tv_words;
        TextView tv_time;
        TextView tv_start_pos;
        TextView tv_end_pos;
        TextView tv_price;
        TextView tv_seats;
        TextView tv_break_appointment_money;
        TextView tv_depart_time;
        Button btn_wei;
        Button btn_buy;
    }

    private class ViewHolder2{
        CircleImage icon;
        TextView phone_style;
        TextView words;
        CheckBox checkBox;
        TextView start_pos;
        TextView end_pos;
        TextView single_price;
        TextView seats;
        TextView depart_time;
        TextView loss;
        AmountView amountView;
        CardView cardView;

        public ViewHolder2(View view) {
            icon=(CircleImage) view.findViewById(R.id.iv_icon);
            phone_style=(TextView)view.findViewById(R.id.tv_phone_and_style);
            words=(TextView)view.findViewById(R.id.tv_words);
            checkBox=(CheckBox)view.findViewById(R.id.checkbox);
            start_pos=(TextView)view.findViewById(R.id.tv_start_pos);
            end_pos=(TextView)view.findViewById(R.id.tv_end_pos);
            single_price=(TextView)view.findViewById(R.id.tv_single_price);
            seats=(TextView)view.findViewById(R.id.tv_transfer_fee);
            depart_time=(TextView)view.findViewById(R.id.tv_depart_time);
            amountView=(AmountView)view.findViewById(R.id.amount_view);
            cardView=(CardView)view.findViewById(R.id.cardview);
            loss=(TextView)view.findViewById(R.id.tv_loss);
        }
    }
    public void setOnTicketChangeListener(OnTicketChangeListener mOnTicketChangeListener){
        this.mOnTicketChangeListener=mOnTicketChangeListener;
    }
    public interface OnTicketChangeListener{
        void onTicketChange(int pre_amount,int amount, int position);
        void onCheckedChange(boolean isChecked,int position);
    }

    public interface OnBuyClickListener{
        void onBuyClick(int position);
    }
    public void setOnBuyTicketListener(OnBuyClickListener mOnBuyClickListener){
        this.mOnBuyClickListener=mOnBuyClickListener;
    }
}
